from rest_framework import serializers
from .models import Note
from django.contrib.auth import get_user_model


class NoteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Note
        fields = (
            "id",
            "body",
            "date",
        )
        read_only_fields = (
            "id",
            "date",
        )

    def create(self, validated_data):
        try:
            Note.objects.get(body=validated_data["body"]).delete()
        except:
            pass
        return super().create(validated_data)


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ("username",)
